package com.hllxd.hism.star.basic.service.service;

import com.hllxd.hism.star.basic.service.dao.mapper.doris.DorisDapanDimDictDefineMapper;
import com.hllxd.hism.star.basic.service.dao.mapper.mis.DapanDimDictDefineMapper;
import com.hllxd.hism.star.basic.service.model.dapan.DapanDimDictDefine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Slf4j
@Service("dimDefineManagerService")
public class DimDefineManagerServiceImpl implements DimDefineManagerService {

    @Autowired
    private DapanDimDictDefineMapper dapanDimDictDefineMapper;
    @Autowired
    private DorisDapanDimDictDefineMapper dorisDapanDimDictDefineMapper;

    @Override
    public List<DapanDimDictDefine> listDimDefine(String search) {
//        DapanDimDictDefineExample defineExample = new DapanDimDictDefineExample();
//        DapanDimDictDefineExample.Criteria criteria = defineExample.createCriteria();
//        criteria.andActionNotEqualTo((byte) 2);
        return dapanDimDictDefineMapper.listDimDefine("%"+search+"%");
    }

    @Override
    public List<DapanDimDictDefine> searchDimDefine(String typeCode) {
        log.info("type code: {}", typeCode);
        return dapanDimDictDefineMapper.searchDimDefine(typeCode);
    }

    @Override
    public DapanDimDictDefine getDimDefine(Long id) {
        return dapanDimDictDefineMapper.selectByPrimaryKey(id);
    }

    @Override
    public long setDimDefine(DapanDimDictDefine it) {
        if (it.getId() != null && it.getId() != 0) {
            it.setAction((byte) 1);
            it.setUpdateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//            dorisDapanDimDictDefineMapper.updateByPrimaryKeySelective(it);
            return dapanDimDictDefineMapper.updateByPrimaryKeySelective(it);
        } else {
            int id = dapanDimDictDefineMapper.insertSelective(it);
            it.setId(it.getId());
            it.setAction((byte) 0);
            it.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            it.setUpdateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            dorisDapanDimDictDefineMapper.insertSelective(it);
            return it.getId();
        }
    }

    @Override
    public long delDimDefine(Long id) {
        DapanDimDictDefine define = new DapanDimDictDefine();
        define.setAction((byte) 2);
        define.setId(id);
        dapanDimDictDefineMapper.updateByPrimaryKeySelective(define);
        dorisDapanDimDictDefineMapper.deleteByPrimaryKey(id);
        return define.getId();
    }

    @Override
    public int delDimDefineByCondition(String typeCode, String itemKey) {
        int i = dapanDimDictDefineMapper.deleteByCondition(typeCode, itemKey);
        dorisDapanDimDictDefineMapper.deleteByCondition(typeCode, itemKey);
        return i;
    }
}
